home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / bfd.mips / section.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-23  |  15.0 KB  |  574 lines

  1. /* Object file "section" support for the BFD library.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*doc*
  22. @section Sections
  23. Sections are supported in bfd in @code{section.c}.
  24.  
  25. The raw data contained within a bfd is maintained through the section
  26. abstraction.  A single bfd may have any number of sections, and keeps
  27. hold of them by pointing to the first, each one points to the next in
  28. the list.
  29.  
  30. @menu
  31. * Section Input::
  32. * Section Output::
  33. * typedef asection::
  34. * section prototypes::
  35. @end menu
  36.  
  37. @node Section Input, Section Output,,Sections
  38. @comment  node-name,  next,  previous,  up
  39. @subsection Section Input
  40. When a bfd is opened for reading, the section structures are created
  41. and attatched to the bfd.
  42.  
  43. Each section has a name which describes the section in the outside
  44. world - for example, @code{a.out} would contain at least three
  45. sections, called @code{.text}, @code{.data} and @code{.bss}. 
  46.  
  47. Sometimes a bfd will contain more than the 'natural' number of
  48. sections. A back end may attatch other sections containing constructor
  49. data, or an application may add a section (using bfd_make_section) to
  50. the sections attatched to an already open bfd. For example, the linker
  51. creates a supernumary section @code{COMMON} for each input file's bfd
  52. to hold information about common storage.
  53.  
  54. The raw data is not necessarily read in at the same time as the
  55. section descriptor is created. Some targets may leave the data in
  56. place until a @code{bfd_get_section_contents} call is made. Other back
  57. ends may read in all the data at once - For example; an S-record file
  58. has to be read once to determine the size of the data. An IEEE-695
  59. file doesn't contain raw data in sections, but data and relocation
  60. expressions intermixed, so the data area has to be parsed to get out
  61. the data and relocations.
  62.  
  63. @node Section Output,typedef asection,Section Input,Sections
  64. @subsection Section Output
  65. To write a new object style bfd, the various sections to be written
  66. have to be created. They are attatched to the bfd in the same way as
  67. input sections, data is written to the sections using
  68. @code{bfd_set_section_contents}. 
  69.  
  70. The linker uses the fields @code{output_section} and
  71. @code{output_offset} to create an output file.
  72.  
  73. The data to be written comes from input sections attatched to the
  74. output sections.  The output section structure can be considered a
  75. filter for the input section, the output section determines the vma of
  76. the output data and the name, but the input section determines the
  77. offset into the output section of the data to be written.
  78.  
  79. Eg to create a section "O", starting at 0x100, 0x123 long, containing two
  80. subsections, "A" at offset 0x0 (ie at vma 0x100) and "B" at offset
  81. 0x20 (ie at vma 0x120) the structures would look like:
  82.  
  83. *+
  84.  
  85.    section name          "A"
  86.      output_offset   0x00
  87.      size            0x20
  88.      output_section ----------->  section name    "O"
  89.                              |    vma             0x100
  90.    section name          "B" |    size            0x123
  91.      output_offset   0x20    |
  92.      size            0x103   |
  93.      output_section  --------|
  94.  
  95. *-
  96.  
  97. */
  98.  
  99.  
  100. #include "sysdep.h"
  101. #include "bfd.h"
  102. #include "libbfd.h"
  103.  
  104.  
  105. /*doc*
  106. @node typedef asection,section prototypes,Section Output,Sections
  107. @subsection typedef asection
  108. */
  109.  
  110. /*proto*
  111. The shape of a section struct:
  112.  
  113. *+++
  114.  
  115. $typedef struct sec {
  116.  
  117. The name of the section, the name isn't a copy, the pointer is
  118. the same as that passed to bfd_make_section.
  119.  
  120. $    CONST char *name;
  121.  
  122. The next section in the list belonging to the bfd, or NULL.
  123.  
  124. $    struct sec *next;
  125.  
  126. The field flags contains attributes of the section. Some of these
  127. flags are read in from the object file, and some are synthesized from
  128. other information. 
  129.  
  130. $flagword flags;
  131.  
  132.  
  133. $#define SEC_NO_FLAGS   0x000
  134.  
  135. Tells the OS to allocate space for this section when loaded.
  136. This would clear for a section containing debug information only.
  137.  
  138. $#define SEC_ALLOC      0x001
  139.  
  140. Tells the OS to load the section from the file when loading.
  141. This would be clear for a .bss section 
  142.  
  143. $#define SEC_LOAD       0x002
  144.  
  145. The section contains data still to be relocated, so there will be some
  146. relocation information too.
  147.  
  148. $#define SEC_RELOC      0x004
  149.  
  150. Obsolete ? 
  151.  
  152. $#define SEC_BALIGN     0x008
  153.  
  154. A signal to the OS that the section contains read only data.
  155.  
  156. $#define SEC_READONLY   0x010
  157.  
  158. The section contains code only.
  159.  
  160. $#define SEC_CODE       0x020
  161.  
  162. The section contains data only.
  163.  
  164. $#define SEC_DATA        0x040
  165.  
  166. The section will reside in ROM.
  167.  
  168. $#define SEC_ROM        0x080
  169.  
  170. The section contains constructor information. This section type is
  171. used by the linker to create lists of constructors and destructors
  172. used by @code{g++}. When a back end sees a symbol which should be used
  173. in a constructor list, it creates a new section for the type of name
  174. (eg @code{__CTOR_LIST__}), attatches the symbol to it and builds a
  175. relocation. To build the lists of constructors, all the linker has to
  176. to is catenate all the sections called @code{__CTOR_LIST__} and
  177. relocte the data contained within - exactly the operations it would
  178. peform on standard data.
  179.  
  180. $#define SEC_CONSTRUCTOR 0x100
  181.  
  182. The section has contents - a bss section could be
  183. @code{SEC_ALLOC} | @code{SEC_HAS_CONTENTS}, a debug section could be
  184. @code{SEC_HAS_CONTENTS}
  185.  
  186. $#define SEC_HAS_CONTENTS 0x200
  187.  
  188. An instruction to the linker not to output sections containing
  189. this flag even if they have information which would normally be written.
  190.  
  191. $#define SEC_NEVER_LOAD 0x400
  192.  
  193. The base address of the section in the address space of the target.
  194.  
  195. $   bfd_vma vma;
  196.  
  197. The size of the section in bytes of the loaded section. This contains
  198. a value even if the section has no contents (eg, the size of @code{.bss}).
  199.  
  200. $   bfd_size_type size;    
  201.  
  202. If this section is going to be output, then this value is the
  203. offset into the output section of the first byte in the input
  204. section. Eg, if this was going to start at the 100th byte in the
  205. output section, this value would be 100. 
  206.  
  207. $   bfd_vma output_offset;
  208.  
  209. The output section through which to map on output.
  210.  
  211. $   struct sec *output_section;
  212.  
  213. The alignment requirement of the section, as an exponent - eg 3
  214. aligns to 2^3 (or 8) 
  215.  
  216. $   unsigned int alignment_power;
  217.  
  218. If an input section, a pointer to a vector of relocation records for
  219. the data in this section.
  220.  
  221. $   struct reloc_cache_entry *relocation;
  222.  
  223. If an output section, a pointer to a vector of pointers to
  224. relocation records for the data in this section.
  225.  
  226. $   struct reloc_cache_entry **orelocation;
  227.  
  228. The number of relocation records in one of the above 
  229.  
  230. $   unsigned reloc_count;
  231.  
  232. Which section is it 0..nth     
  233.  
  234. $   int index;                      
  235.  
  236. Information below is back end specific - and not always used or
  237. updated 
  238.  
  239. File position of section data   
  240.  
  241. $   file_ptr filepos;      
  242. File position of relocation info        
  243.  
  244. $   file_ptr rel_filepos;
  245.  
  246. File position of line data              
  247.  
  248. $   file_ptr line_filepos;
  249.  
  250. Pointer to data for applications        
  251.  
  252. $   PTR userdata;
  253.  
  254. $   struct lang_output_section *otheruserdata;
  255.  
  256. Attached line number information        
  257.  
  258. $   alent *lineno;
  259. Number of line number records   
  260.  
  261. $   unsigned int lineno_count;
  262.  
  263. When a section is being output, this value changes as more
  264. linenumbers are written out 
  265.  
  266. $   file_ptr moving_line_filepos;
  267.  
  268. what the section number is in the target world 
  269.  
  270. $   unsigned int target_index;
  271.  
  272. $   PTR used_by_bfd;
  273.  
  274. If this is a constructor section then here is a list of the
  275. relocations created to relocate items within it.
  276.  
  277. $   struct relent_chain *constructor_chain;
  278.  
  279. The bfd which owns the section.
  280.  
  281. $   bfd *owner;
  282.  
  283. $} asection ;
  284.  
  285. *---
  286.  
  287. */
  288.  
  289. /*doc*
  290. @node section prototypes,Section,typedef section,Sections
  291. @subsection section prototypes
  292.  
  293. */
  294. /*proto* bfd_get_section_by_name
  295. Runs through the provided @var{abfd} and returns the @code{asection}
  296. who's name matches that provided, otherwise NULL. @xref{Sections}, for more information.
  297.  
  298. *; PROTO(asection *, bfd_get_section_by_name,
  299.     (bfd *abfd, CONST char *name));
  300. */
  301. asection *
  302. DEFUN(bfd_get_section_by_name,(abfd, name),
  303.       bfd *abfd AND
  304.       CONST char *name)
  305. {
  306.   asection *sect;
  307.  
  308.   for (sect = abfd->sections; sect != NULL; sect = sect->next)
  309.     if (!strcmp (sect->name, name)) return sect;
  310.   return NULL;
  311. }
  312.  
  313.  
  314. /*proto* bfd_make_section
  315. This function creates a new empty section called @var{name} and attatches it
  316. to the end of the chain of sections for @var{bfd}. An attempt to
  317. create a section with a name which is already in use, returns the old
  318. section by that name instead.
  319.  
  320. Possible errors are:
  321. @table @code
  322. @item invalid_operation
  323. If output has already started for this bfd.
  324. @item no_memory
  325. If obstack alloc fails.
  326. @end table
  327.  
  328. *; PROTO(asection *, bfd_make_section, (bfd *, CONST char *name));
  329. */
  330.  
  331.  
  332.  
  333. sec_ptr
  334. DEFUN(bfd_make_section,(abfd, name),
  335.       bfd *abfd AND
  336.       CONST char * name)
  337. {
  338.   asection *newsect;  
  339.   asection **  prev = &abfd->sections;
  340.   asection * sect = abfd->sections;
  341.   
  342.   if (abfd->output_has_begun) {
  343.     bfd_error = invalid_operation;
  344.     return NULL;
  345.   }
  346.  
  347.   while (sect) {
  348.     if (!strcmp(sect->name, name)) return sect;
  349.     prev = §->next;
  350.     sect = sect->next;
  351.   }
  352.  
  353.   newsect = (asection *) bfd_zalloc(abfd, sizeof (asection));
  354.   if (newsect == NULL) {
  355.     bfd_error = no_memory;
  356.     return NULL;
  357.   }
  358.  
  359.   newsect->name = name;
  360.   newsect->index = abfd->section_count++;
  361.   newsect->flags = SEC_NO_FLAGS;
  362.  
  363.   newsect->userdata = 0;
  364.   newsect->next = (asection *)NULL;
  365.   newsect->relocation = (arelent *)NULL;
  366.   newsect->reloc_count = 0;
  367.   newsect->line_filepos =0;
  368.   newsect->owner = abfd;
  369.   if (BFD_SEND (abfd, _new_section_hook, (abfd, newsect)) != true) {
  370.     free (newsect);
  371.     return NULL;
  372.   }
  373.  
  374.   *prev = newsect;
  375.   return newsect;
  376. }
  377.  
  378.  
  379. /*proto* bfd_set_section_flags
  380. Attempts to set the attributes of the section named in the bfd
  381. supplied to the value. Returns true on success, false on error.
  382. Possible error returns are:
  383. @table @code
  384. @item invalid operation
  385. The section cannot have one or more of the attributes requested. For
  386. example, a .bss section in @code{a.out} may not have the
  387. @code{SEC_HAS_CONTENTS} field set.
  388. @end table
  389.  
  390. *; PROTO(boolean, bfd_set_section_flags,
  391.        (bfd *, asection *, flagword));
  392. */
  393.  
  394. boolean
  395. DEFUN(bfd_set_section_flags,(abfd, section, flags),
  396.      bfd *abfd AND
  397.      sec_ptr section AND
  398.      flagword flags)
  399. {
  400.   if ((flags & bfd_applicable_section_flags (abfd)) != flags) {
  401.     bfd_error = invalid_operation;
  402.     return false;
  403.   }
  404.  
  405.   section->flags = flags;
  406.   return true;
  407. }
  408.  
  409.  
  410. /*proto* bfd_map_over_sections
  411. Calls the provided function @var{func} for each section attatched to
  412. the bfd @var{abfd}, passing @var{obj} as an argument. The function
  413. will be called as if by 
  414.  
  415. @example
  416.   func(abfd, the_section, obj);
  417. @end example
  418.  
  419.  
  420. *; PROTO(void, bfd_map_over_sections,
  421.             (bfd *abfd, void (*func)(), PTR obj));
  422.  
  423. This is the prefered method for iterating over sections, an
  424. alternative would be to use a loop:
  425.  
  426. @example
  427.    section *p;
  428.    for (p = abfd->sections; p != NULL; p = p->next)
  429.       func(abfd, p, ...)
  430. @end example
  431. */
  432.  
  433. /*VARARGS2*/
  434. void
  435. DEFUN(bfd_map_over_sections,(abfd, operation, user_storage),
  436.       bfd *abfd AND
  437.       void (*operation)() AND
  438.       PTR user_storage)
  439. {
  440.   asection *sect;
  441.   int i = 0;
  442.   
  443.   for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
  444.     (*operation) (abfd, sect, user_storage);
  445.  
  446.   if (i != abfd->section_count)         /* Debugging */
  447.     abort();
  448. }
  449.  
  450.  
  451. /*proto* bfd_set_section_size
  452. Sets @var{section} to the size @var{val}. If the operation is ok, then
  453. @code{true} is returned, else @code{false}. 
  454.  
  455. Possible error returns:
  456. @table @code
  457. @item invalid_operation
  458. Writing has started to the bfd, so setting the size is invalid
  459. @end table 
  460.  
  461. *; PROTO(boolean, bfd_set_section_size,
  462.      (bfd *, asection *, bfd_size_type val));
  463. */
  464.  
  465. boolean
  466. DEFUN(bfd_set_section_size,(abfd, ptr, val),
  467.       bfd *abfd AND
  468.       sec_ptr ptr AND
  469.       bfd_size_type val)
  470. {
  471.   /* Once you've started writing to any section you cannot create or change
  472.      the size of any others. */
  473.  
  474.   if (abfd->output_has_begun) {
  475.     bfd_error = invalid_operation;
  476.     return false;
  477.   }
  478.  
  479.   ptr->size = val;
  480.   
  481.   return true;
  482. }
  483.  
  484. /*proto* bfd_set_section_contents
  485. Sets the contents of the section @var{section} in bfd @var{abfd} to
  486. the data starting in memory at @var{data}. The data is written to the
  487. output section starting at offset @var{offset} for @var{count} bytes.
  488.  
  489. Normally @code{true} is returned, else @code{false}. Possible error
  490. returns are:
  491. @table @code
  492. @item no_contents
  493. The output section does not have the @code{SEC_HAS_CONTENTS}
  494. attribute, so nothing can be written to it.
  495. @item and some more too
  496. @end table
  497. This routine is front end to the back end function @code{_bfd_set_section_contents}.
  498.  
  499. *; PROTO(boolean, bfd_set_section_contents,
  500.          (bfd *abfd,        
  501.          asection *section,
  502.          PTR data,
  503.          file_ptr offset,
  504.          bfd_size_type count));
  505.  
  506. */
  507.  
  508. boolean
  509. DEFUN(bfd_set_section_contents,(abfd, section, location, offset, count),
  510.       bfd *abfd AND
  511.       sec_ptr section AND
  512.       PTR location AND
  513.       file_ptr offset AND
  514.       bfd_size_type count)
  515. {
  516.   if (!(bfd_get_section_flags(abfd, section) & SEC_HAS_CONTENTS)) 
  517.       {
  518.         bfd_error = no_contents;
  519.         return(false);
  520.       } 
  521.  
  522.   if (BFD_SEND (abfd, _bfd_set_section_contents,
  523.                 (abfd, section, location, offset, count))) 
  524.       {
  525.         abfd->output_has_begun = true;
  526.         return true;
  527.       }
  528.  
  529.   return false;
  530. }
  531.  
  532. /*proto* bfd_get_section_contents
  533. This function reads data from @var{section} in bfd @var{abfd} into
  534. memory starting at @var{location}. The data is read at an offset of
  535. @var{offset} from the start of the input section, and is read for
  536. @var{count} bytes.
  537.  
  538. If the contents of a constuctor with the @code{SEC_CONSTUCTOR} flag
  539. set are requested, then the @var{location} is filled with zeroes.
  540.  
  541. If no errors occur, @code{true} is returned, else @code{false}.
  542. Possible errors are:
  543.  
  544. @table @code
  545. @item unknown yet
  546. @end table
  547.  
  548. *; PROTO(boolean, bfd_get_section_contents, 
  549.         (bfd *abfd, asection *section, PTR location,
  550.          file_ptr offset, bfd_size_type count));
  551.  
  552.  
  553. */
  554. boolean
  555. DEFUN(bfd_get_section_contents,(abfd, section, location, offset, count),
  556.       bfd *abfd AND
  557.       sec_ptr section AND
  558.       PTR location AND
  559.       file_ptr offset AND
  560.       bfd_size_type count)
  561. {
  562.   if (section->flags & SEC_CONSTRUCTOR) 
  563.       {
  564.         memset(location, 0, (unsigned)count);
  565.         return true;
  566.       }
  567.   else 
  568.       {
  569.         return  (BFD_SEND (abfd, _bfd_get_section_contents,
  570.                            (abfd, section, location, offset, count)));
  571.       }
  572. }
  573.  
  574.